 package com.alibaba.org.controller;

import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.alibaba.org.domain.Department;
import com.alibaba.org.query.DepartmentQuery;
import com.alibaba.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//@PathVariable  取URL中占位符的值,将值赋值给方法的参数
//@RestController 将结果封装成json对象 放到响应体中返回
//@RequestBody 从请求体中获取参数

 /**写接口的三要素
  * 请求地址是什么
  * 请求的参数是什么
  * 返回值是什么
  */

@RestController//前后端传递参数使用json对象  @ResponseBody + @controller
@RequestMapping( "/department") //http://localhost:8080/dept

public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    //新增或修改部门   put
    //删除一条记录  delete
//    @DeleteMapping("/{id}")
//    public AjaxResult removeById(@PathVariable("id") long id){
//        //ctrl + alt + t
//        try {
//            departmentService.remove(id);
//            return  AjaxResult.me();
//        } catch (Exception e) {
//            e.printStackTrace();
//         1. AjaxResult ajaxResult = new AjaxResult();
//            ajaxResult.setSuccess(Boolean.FALSE);
//            ajaxResult.setMessage("系统繁忙,请稍后重试！");
//            return ajaxResult;
//         2. return new AjaxResult().setSuccess(false).setMessage("系统繁忙,请稍后重试！");
//            return AjaxResult.me()
//                    .setSuccess(false)
//                    .setMessage("系统繁忙,请稍后重试！");
//        }
//    }
     /**
      * 接口：删除
      * @param id
      * @return
      */
     @DeleteMapping("/{id}")
     //http://localhost:8080/department/1 + delete
     public AjaxResult delete(@PathVariable("id") Long id){
         try {
             departmentService.remove(id);
             return new AjaxResult();//true + "操作成功"
         } catch (Exception e) {
             e.printStackTrace();
             return new AjaxResult(false,"删除失败");
             //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
         }
     }

     /**
      * 接口：批量删除
      *  批量操作 foreach
      */
     @PatchMapping
     //http://localhost:8080/department/1 + delete
     public AjaxResult pathDelete(@RequestBody Long[] ids){ //参数数组 集合 皆可用
         try {
             departmentService.pathDelete(ids);
             return new AjaxResult();//true + "操作成功"
         } catch (Exception e) {
             e.printStackTrace();
             return new AjaxResult( false,"删除失败");
             //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
         }
     }


     @PutMapping
     public AjaxResult addOrUpdate(@RequestBody Department department){

         try {
             if(department.getId() == null){
                 departmentService.save(department);
             }else{
                 departmentService.update(department);
             }
             return new AjaxResult();
         } catch (Exception e) {
             e.printStackTrace();
             return AjaxResult.me()
                   .setSuccess(false)
                   .setMessage("系统繁忙,请稍后重试！");

         }
     }

     //查询一条记录  get
    @GetMapping("/{id}")
    public  Department queryById(@PathVariable("id") Long id){
       return departmentService.loadById(id);
    }
    //查询所有记录  get
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.loadAll();
    }

    //分页查询 + 高级查询 post
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }
     /**
      * 查询部门树
      * @return
      */
     @GetMapping("/deptTree") // http://localhost:8080/department + get  与上面的请求参数不同所以请求方式是不一样的
     public List<Department> getDeptTree(){
         return departmentService.getDeptTree();
     }
}
